/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.yunyichina.provider.framework.annotation.validate;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Validate the @ValidateSize annotation
 *
 * @author jiangtch
 */
public class ValidateSizeHandler implements Handler {

    private static final Logger logger = LoggerFactory.getLogger(ValidateSizeHandler.class);

    public void validate(AnnotationValidable validatedObj, Field field)
            throws ValidateException {
        if (logger.isTraceEnabled()) {
            logger.trace(this.getClass().getName(), "validate()");
        }
        if (field.isAnnotationPresent(ValidateSize.class)) {
            checkSize(validatedObj, field);
        }
    }

    /**
     * validate the String length
     *
     * @param filter interceptor validated object
     * @param field  validated field or property
     * @throws ValidateException
     */
    private void checkSize(AnnotationValidable filter, Field field)
            throws ValidateException {
        if (logger.isTraceEnabled()) {
            logger.trace(this.getClass().getName(), "checkSize()");
        }
        ValidateSize annotation = field.getAnnotation(ValidateSize.class);
        String minSize = annotation.minSize();
        String maxSize = annotation.maxSize();
        int min = 0;
        int max = 0;
        if (!"".equals(minSize)) {
            min = Integer.parseInt(minSize);
        }
        if (!"".equals(maxSize)) {
            max = Integer.parseInt(maxSize);
        }
        
        Object valueObject;
        try {
            valueObject = GetFiledValue.getField(filter, field.getName());
        } catch (Exception ex) {
            logger.error("字段：" + field.getName() + " 获取值发生异常->" + ex.getMessage(), ex);
            throw new ValidateException("字段：" + field.getName() + " 获取值发生异常->" + ex.getMessage(), ex);
        }
        
        if(valueObject==null){
            return;
        }
        
        String value=String.valueOf(valueObject);
        int size = 0;
        if (!"".equals(value) && value != null) {
            size = value.length();
        }
        if (!"".equals(minSize) && !"".equals(maxSize)) {
            if (size < min || size > max) {
                logger.warn("字段：" + field.getName() + "校验失败. 当前值长度:" + size + ",最小长度:" + min + ",最大长度:" + max);
                throw new ValidateException("字段：" + field.getName() + "校验失败. 当前值长度:" + size + ",最小长度:" + min + ",最大长度:" + max);
            }
        }
        if (!"".equals(minSize) && "".equals(maxSize)) {
            if (size < min) {
                logger.warn("字段：" + field.getName() + "校验失败. 当前值长度:" + size + ",最小长度:" + min);
                throw new ValidateException("字段：" + field.getName() + "校验失败. 当前值长度:" + size + ",最小长度:" + min);
            }
        }
        if ("".equals(minSize) && !"".equals(maxSize)) {
            if (size > max) {
                logger.warn("字段：" + field.getName() + "校验失败. 当前值长度:" + size + ",最大长度:" + max);
                throw new ValidateException("字段：" + field.getName() + "校验失败. 当前值长度:" + size + ",最大长度:" + max);
            }
        }
    }

}
